package sale.wxb.loveshopping.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import sale.wxb.loveshopping.entity.common.Result;
import sale.wxb.loveshopping.entity.model.SysUser;
import sale.wxb.loveshopping.entity.vo.common.BatchIdsBody;
import sale.wxb.loveshopping.entity.vo.common.PageListParam;
import sale.wxb.loveshopping.entity.vo.common.PageRecord;
import sale.wxb.loveshopping.entity.vo.user.AddUserVo;
import sale.wxb.loveshopping.entity.vo.user.SysUserVo;
import sale.wxb.loveshopping.entity.vo.user.UpdateUserVo;
import sale.wxb.loveshopping.service.ISysUserService;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import java.util.Collections;

@RestController
@RequestMapping("/user")
@Validated
@Api(tags = "用户管理")
public class UserController {
    @Autowired
    private ISysUserService sysUserService;

    @GetMapping("getUserInfoByToken")
    @ApiOperation("按Token获取用户信息")
    public Result<SysUser> getUserInfoByToken() {
        return Result.success(sysUserService.getUserByToken());
    }

    @GetMapping("getUserInfoById")
    @ApiOperation("按id获取用户信息")
    public Result<SysUser> getUserInfoById(@RequestParam Long id) {
        return Result.success(sysUserService.getById(id));
    }

    @GetMapping("getUserByAccount")
    @ApiOperation("按账号获取用户信息")
    public Result<SysUser> getUserByAccount(@RequestParam String account) {
        return Result.success(sysUserService.getUserByAccount(account));
    }

    @GetMapping("getUserByEmail")
    @ApiOperation("按邮箱获取用户信息")
    public Result<SysUser> getUserByEmail(@RequestParam @Email(message = "邮箱格式不合法") String email) {
        return Result.success(sysUserService.getUserByEmail(email));
    }

    @PostMapping("addUser")
    @ApiOperation(value = "添加用户")
    public Result<Long> addUser(@RequestBody @Validated AddUserVo body) {
        return Result.success(sysUserService.createAccount(body));
    }

    @PostMapping("updateUser")
    @ApiOperation(value = "按id更新用户")
    public Result<Long> updateUserById(@RequestBody @Validated UpdateUserVo body) {
        return Result.success(sysUserService.updateUserById(body));
    }

    @PostMapping("updateUserByToken")
    @ApiOperation(value = "按Token更新当前用户")
    public Result<Boolean> updateUserByToken(@RequestBody @Validated SysUserVo body) {
        return Result.success(sysUserService.updateUserByToken(body));
    }

    @PostMapping("batchRemoveUserByIds")
    @ApiOperation("按id批量删除用户")
    public Result<Boolean> batchRemoveUserByIds(@RequestBody BatchIdsBody body) {
        return Result.send(sysUserService.removeBatchByIds(body.getIds()));
    }

    @GetMapping("removeUserById")
    @ApiOperation("按id删除用户")
    public Result<Boolean> removeUserById(@RequestParam @NotNull @Positive Long id) {
        return Result.send(sysUserService.removeBatchByIds(Collections.singletonList(id)));
    }

    @GetMapping("list")
    @ApiOperation("查询用户列表")
    public Result<PageRecord<SysUser>> list(@Validated PageListParam param) {
        return Result.success(sysUserService.getListByPage(param));
    }
}
